﻿using Abp.AutoMapper;
using Abp.ElasticSearch;
using Abp.ElasticSearch.Configuration;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Runtime.Caching.Redis;
using AbpBuild.Authorization;
using AbpBuild.Configuration;
using Microsoft.Extensions.Configuration;
using System;

namespace AbpBuild
{
    [DependsOn(
        typeof(AbpBuildCoreModule), 
        typeof(AbpAutoMapperModule),
        typeof(AbpElasticSearchModule),
        typeof(AbpRedisCacheModule))]
    public class AbpBuildApplicationModule : AbpModule
    {
        private static IConfigurationRoot _appConfiguration = AppConfigurations.Get(System.Environment.CurrentDirectory);

        public override void PreInitialize()
        {
            Configuration.Modules.ElasticSearch().ConnectionString = _appConfiguration.GetConnectionString("ESapi");
            Configuration.Modules.ElasticSearch().AuthUserName = "elastic";
            Configuration.Modules.ElasticSearch().AuthPassWord = "password.123";

            Configuration.Authorization.Providers.Add<AbpBuildAuthorizationProvider>();

            Configuration.Caching.UseRedis(options =>
            {
                options.ConnectionString = _appConfiguration.GetConnectionString("RedisConnection");
                options.DatabaseId = Convert.ToInt32(_appConfiguration.GetConnectionString("DatabaseId"));
            });
        }

        public override void Initialize()
        {
            var thisAssembly = typeof(AbpBuildApplicationModule).GetAssembly();

            IocManager.RegisterAssemblyByConvention(thisAssembly);

            Configuration.Modules.AbpAutoMapper().Configurators.Add(
                // Scan the assembly for classes which inherit from AutoMapper.Profile
                cfg => cfg.AddMaps(thisAssembly)
            );
        }
    }
}
